#include #include using namespace std; void displayGameBoard(int xMoves, int oMoves) { for(int i = 1; i <= 9;i++) { if((xMoves & (int)pow(2,i)) == (int)pow(2,i)) { cout << 'X'; } else if((oMoves & (int)pow(2,i)) == (int)pow(2,i)) { cout << 'O'; } else { cout << i; } if(i == 3 || i == 6) { cout << "\n-----\n"; } if(i %3 != 0) { cout << "|"; } } cout << endl; } void displayCurrentPlayerPrompt(char currentPlayer) { cout << currentPlayer << "'s turn:\n"; } int getValidMove(int xMoves, int oMoves) { int result; do { cin >> result; } while((((int)pow(2,result) & xMoves) == (int)pow(2,result)) || (((int)pow(2,result) & oMoves) == (int)pow(2,result)) || result < 1 || result > 9); return result; } int max(int x, int y) { int result = x; if(y > x) { result = y; } return result; } void findMax(int x, int y, int& max) { max = x; if(y > x) { max = y; } } //int max(int x, int y) //{ // if(y > x) // { // return y; // } // return x; //} void applyMove(int currentMove,int& xMoves, int& oMoves, char currentPlayer) { if(currentPlayer == 'X') { //(int) - integer type cast xMoves = (xMoves | (int)(pow(2,currentMove))); } else { //(int) - integer type cast oMoves = (oMoves | (int)(pow(2,currentMove))); } } //& - to receive the parameter by reference //by value - is when a copy is made void changePlayer(char& currentPlayer) { //change whose turn it is if(currentPlayer == 'X') { currentPlayer = 'O'; } else { currentPlayer = 'X'; } } //void - verb - verb noun combinations //non void functions - noun, adjective noun void main() { int a; int b; int c; cin >> a >> b; c = max(a,b); findMax(a,b,c); //winning combinations are 168,14,112,896,146,292,584,546 //1022 int xMoves = 0; int oMoves = 0; char currentPlayer = 'X'; bool gameOver = false; int move; while(!gameOver) { displayCurrentPlayerPrompt(currentPlayer); displayGameBoard(xMoves,oMoves); move = getValidMove(xMoves, oMoves); applyMove(move,xMoves,oMoves,currentPlayer); changePlayer(currentPlayer); } }